<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="病患姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="patientIdcard">
        <el-input
          v-model="queryParams.patientIdcard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="patientAge">
        <el-input
          v-model="queryParams.patientAge"
          placeholder="请输入年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="doctorTel">
        <el-input
          v-model="queryParams.doctorTel"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="doctorAdd">
        <el-input
          v-model="queryParams.doctorAdd"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入时间" prop="registrationStarttime">
        <el-date-picker clearable
                        v-model="queryParams.registrationStarttime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择录入时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预约时间" prop="registrationEndtime">
        <el-date-picker clearable
                        v-model="queryParams.registrationEndtime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择预约时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="挂号费用" prop="registrationMoney">
        <el-input
          v-model="queryParams.registrationMoney"
          placeholder="请输入挂号费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="医师" prop="doctorId">

        <el-select
          v-model="doctorId"
          placeholder="请选择医师"
          @change="doctorChange(doctorId)"
        >
          <el-option
            v-for="item in doctorLists"
            :label="item.doctorName"
            :value="item.doctorId"
          >
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="科室" prop="deptId">

        <el-select
          v-model="departmentid"
          placeholder="请选择科室"
          @change="deptLikeChange(departmentid)"
        >
          <el-option
            v-for="item in departmentlist"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['outpatient:registration:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['outpatient:registration:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['outpatient:registration:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['outpatient:registration:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getLists"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registrationList" @selection-change="handleSelectionChange">
      <el-table-column label="科室名称" align="center" prop="deptName"/>
      <el-table-column label="排队号" align="center" prop="sortNum"/>
      <el-table-column label="病患姓名" align="center" prop="patientName"/>
      <el-table-column label="联系方式" align="center" prop="doctorTel"/>
      <el-table-column label="地址" align="center" prop="doctorAdd"/>
      <el-table-column label="挂号时间" align="center" prop="registrationStarttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationStarttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="医师姓名" align="center" prop="doctorName"/>

      <el-table-column label="状态" align="center" prop="state"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="big"
            type="text"
            icon="el-icon-add"
            @click="changeListStateAndAddPatient(scope.row)"
            v-hasPermi="['outpatient:checks:remove']"
          >接诊</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getLists"
    />

    <!-- 添加或修改挂号流水表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="病患姓名:" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入病患姓名"/>
        </el-form-item>
        <el-form-item label="身份证号:" prop="patientIdcard">
          <el-input v-model="form.patientIdcard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="性别:" prop="dickerSex">
          <el-input v-model="form.dickerSex" placeholder="请输入病患性别"/>
        </el-form-item>
        <el-form-item label="年龄:" prop="patientAge">
          <el-input v-model="form.patientAge" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="联系方式:" prop="doctorTel">
          <el-input v-model="form.doctorTel" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="地址:" prop="doctorAdd">
          <el-input v-model="form.doctorAdd" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="录入时间:" prop="registrationStarttime">
          <el-date-picker clearable
                          v-model="form.registrationStarttime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择录入时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间:" prop="registrationEndtime">
          <el-date-picker clearable
                          v-model="form.registrationEndtime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预约时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="挂号费用:" prop="registrationMoney">
          <el-input v-model="form.registrationMoney" placeholder="请输入挂号费用"/>
        </el-form-item>

        <div class="block">
          <el-form-item label="选择科室/医师:">
            <el-select
              v-model="departmentid"
              placeholder="请选择部门"
              @change="changeset"
            >
              <el-option
                v-for="item in departmentlist"
                :key="item.value"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>

            <el-select v-model="personid" placeholder="请选择人员" :disabled="disabled" @change="doctorWork(personid)">
              <el-option
                v-for="item in personlist"
                :key="item.value"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>


          </el-form-item>
        </div>

        <el-form-item label="状态:" prop="state">
          <el-select v-model="value" placeholder="请选择状态" @change="sss(value)">
            <el-option
              v-for="item in PatientStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import {
  getRegistration,
  delRegistration,
  addRegistration,
  updateRegistration,
  getdeptList,
  getDoctorList,
  listRegistrations,
  getDoctorLikeList,
  changeListState,
  addPatient
} from '@/api/outpatient/daizhen'
import item from '@/layout/components/Sidebar/Item.vue'

export default {
  name: 'Registration',
  data() {
    return {
      doctorId:'',
      doctorLists:[],


      value:'',
      PatientStatus:[
        {
          value:'未缴费',
          label:'未缴费'
        },
        {
          value:'已缴费',
          label:'已缴费'
        },
        {
          value:'已就诊',
          label:'已就诊'
        }
      ],

      //部门
      departmentlist: [],
      departmentid: null,
      //人员
      personlist: [],
      personid: null,


      //禁用
      disabled: true,

      deptDoct: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号流水表表格数据
      registrationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        dickerSex: null,
        patientIdcard: null,
        patientAge: null,
        doctorTel: null,
        doctorAdd: null,
        registrationStarttime: null,
        registrationEndtime: null,
        registrationMoney: null,
        doctorId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {

    this.getdepartmentlist()
    // this.getList()
    this.getLists()

    this.getDoctorLists()

  },
  methods: {

    //接诊，往病历单加东西，改挂号表状态
    changeListStateAndAddPatient(row){

      if(row.state === '已接诊'){
        this.$modal.msgError('已经接诊过了就不能接诊了偶~')
        return;
      }
      changeListState(row).then(response => {
      }),
        addPatient(row).then(response => {
          this.$modal.msgSuccess('接诊成功~')
          this.getLists();
        })

    },
    deptLikeChange(id){
      this.queryParams.deptId = id
    },
    getDoctorLists(){
      getDoctorLikeList().then(response => {
        this.doctorLists = response.data
        this.loading = false
      })
    },
    doctorChange(doctorId){
      this.queryParams.doctorId = doctorId
    },
    sss(ss){
      this.form.state = ss
    },
    doctorWork(id){
      this.form.doctorId = id
    },

    getdepartmentlist() {
      getdeptList().then(response => {
        this.departmentlist = response.data.data
        this.loading = false
      })
    },
    changeset() {
      this.form.deptId = this.departmentid
      getDoctorList().then(response => {
        let data = response.data.data
        this.loading = false
        //如果部门id不为空
        if (this.departmentid) {
          //切换的时候人员清空，防止bug
          this.personlist = []
          //禁用取消
          this.disabled = false

          data.forEach(item => {

            if (item.departmentid == this.departmentid) {
              this.personlist.push(item)
            }
          })
        } else {
          //选中的部门为空，则清空人员列表
          this.personlist = []
          this.disabled = true

        }
      })
    },

    /** 查询挂号流水表列表 */
    getLists() {
      this.loading = true
      listRegistrations(this.queryParams).then(response => {
        this.registrationList = response.rows
        console.log(this.registrationList)
        this.total = response.total
        this.loading = false
      })
    },

    // /** 查询挂号流水表列表 */
    // getList() {
    //   this.loading = true
    //   listRegistration(this.queryParams).then(response => {
    //     this.registrationList = response.rows
    //     this.total = response.total
    //     this.loading = false
    //   })
    // },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.departmentid = null
      this.personid = null
    },
    // 表单重置
    reset() {
      this.form = {
        registrationId: null,
        patientName: null,
        dickerSex: null,
        patientIdcard: null,
        patientAge: null,
        doctorTel: null,
        doctorAdd: null,
        registrationStarttime: null,
        registrationEndtime: null,
        registrationMoney: null,
        doctorId: null,
        deptId: null,
        state:null



      }
      this.resetForm('form')
      this.value = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.doctorId=''
      this.departmentid=''
      this.getLists()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.registrationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增挂号表'
    },




    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const registrationId = row.registrationId || this.ids
      getRegistration(registrationId).then(response => {
        this.form = response.data
        this.value = response.data.state
        this.departmentid = this.form.deptId
        this.open = true
        this.title = '修改挂号表'
      })
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.registrationId != null) {

            updateRegistration(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.departmentid = null
              this.personid = null
              this.open = false
              this.getLists()
            })
          } else {
            addRegistration(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.departmentid = null
              this.personid = null
              this.open = false
              this.getLists()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const registrationIds = row.registrationId || this.ids
      this.$modal.confirm('是否确认删除挂号流水表编号为"' + registrationIds + '"的数据项？').then(function() {
        return delRegistration(registrationIds)
      }).then(() => {
        this.getLists()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('outpatient/registration/export', {
        ...this.queryParams
      }, `registration_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
